/*
 * CS_REQ_SLOT_LIST.cpp
 *
 *  Created on: 2013. 9. 6.
 *      Author: jjaehuny
 */

#include "world.h"

#include "../../engine.h"

#include "../../User.h"

#include "../../Enum.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Equip.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Function.h"

#include <time.h>

namespace ProjectZ { namespace NCommand { namespace NWorld {

void CS_REQ_USER_INFO_INVEN::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 *
	 *
	 * // 인벤 정보 (마을입장시 기본으로 요청함)
	CS_REQ_USER_INFO_INVEN		0x0907
	{
	}
	// 인벤 정보
	 *
	SC_REQ_USER_INFO_INVEN		0x0908
	{
		u32	u32gid;
		u32	u32gold;		// 보유 골드

		u8	bagcount MAX 6		// 가방 수
		{
			u8	u8opencount;	// 오픈슬롯갯수
		}

		u8	itemcount MAX 13	// 장착된 아이템 개수
		{
			u8	u8equip_pos;	// 장착위치
			///u8	u8bag_type;		// 가방 타입
			u64	u64item_seq;	// 인벤슬롯번호
		}
	}

	 */

	STRESSLOG("[CHANNEL] CS_REQ_USER_INFO_INVEN::ONEXECUTE");

	PREPARE_PACKET;

	DEBUGLOG("CS_REQ_USER_INFO_INVEN GID : %d", pUser->GetUserSeq());

	// 전체 아이템 정보를 Cache 처리한다....
	for(int i = static_cast<int>(BAG_TYPE_NORMAL); i < static_cast<int>(BAG_TYPE_MAX); ++i)
	{
		NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(i));
		if (NULL == pBag)
			return;

		// Item Load From Database
		if (false == pBag->IsLoad())
		{
			if (false == pBag->ItemLoadFromDatabase())
			{
				DEBUGLOG("CS_REQ_USER_INFO_INVEN ITEMLOADFROMDATABASE FAIL GID : %d", pUser->GetUserSeq());
				return;
			}
			else
			{
				DEBUGLOG("FLOW> CS_REQ_USER_INFO_INVEN()> 12. userseq: %d, bag: %d", pUser->GetUserSeq(), i);
			}
		}
		else
		{
			DEBUGLOG("FLOW> CS_REQ_USER_INFO_INVEN()> 10. the bag is loaded!> userseq: %d, bag: %d", pUser->GetUserSeq(), i);

		}
	}



	DEBUGLOG("FLOW> CS_REQ_USER_INFO_INVEN()> 100. userseq: %d", pUser->GetUserSeq());

	pUser->CacheBagInfo(true);

	NLogic::Equip * pEquip = pUser->GetEquip();
	pEquip->EquipAddFromCharacterInfo( 100 );
	pEquip->UpdateCharacterInfo();

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	* pAck << pUser->GetUserSeq();
	* pAck << pUser->GetCharacterInfo().gold;

	* pAck << static_cast<uint8_t>(BAG_TYPE_MAX);

	for(int i = 0 ; i < BAG_TYPE_MAX ; i++)
	{
		int maxSlotCount = NLogic::NFunction::GetBagSlotMaxOpenCount(static_cast<INVEN_BAG_TYPE>(i));
		pAck->MsgCat("%c", maxSlotCount);
	}

	* pAck << static_cast<uint8_t>(pEquip->HasItemCount());

	if (pEquip->HasItemCount() <= 0)
		DEBUGLOG("INVENINFO EQUIPHASITEMCOUNT IS ZERO GID : %d", pUser->GetUserSeq());

	for (int i = 0; i < static_cast<int>(EnumItemEquipPosition::MAX_ITEM_EQUIP_POS); ++i)
	{
		NLogic::Item * pItem = pEquip->GetItem(static_cast<EnumItemEquipPosition>(i));
		if (NULL == pItem)
			continue;

		* pAck << static_cast<uint8_t>(i);
		* pAck << static_cast<uint64_t>(pItem->GetItemSeq());
		DEBUGLOG("INVENINFO SEND ITEM SEQ : %ld", pItem->GetItemSeq());
	}

	NLogic::Item * pItem = pEquip->GetItem(static_cast<EnumItemEquipPosition>(ITEM_EQUIP_POS_NAME_TAG));
	if (NULL != pItem)
	{
		* pAck << static_cast<uint8_t>(ITEM_EQUIP_POS_NAME_TAG);
		* pAck << static_cast<uint64_t>(pItem->GetItemSeq());
	}

	pAck->MakePacket(CMD_SC_REQ_USER_INFO_INVEN);
	pUser->Send(pAck);
}

} /*NWorld*/ } /*NCommand*/ } /*ProjectZ*/


